package com.xzit.rcpyxt.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: PageUtil</p>
 * <p>Description: 分页工具类</p>
 * @author cyy
 * @date 2018年8月6日
 */
public class PageUtil {
    /**
     * 每页大小
     */
    private int pageSize;

    /**
     * 当前页为第几页
     */
    private int page;

    /**
     * 是否为第一页
     */
    private boolean first = false;

    /**
     * 是否为最后一页
     */
    private boolean last = false;

    /**
     * 总共有多少页
     */
    private int totalPages;

    /**
     * 总共有多少条数据
     */
    private int totalElements;

    /**
     * 当前页一共有多少条数据
     */
    private int numberOfElements;

    /**
     * 数据
     */
    private List<?> content = new ArrayList<>();

    /**
     * 根据传入的当前多少页
     * @param pageSize
     * @param page
     * @param totalElements
     */
    public PageUtil(int page, int pageSize, int totalElements) {

        this.pageSize = pageSize;

        this.totalElements = totalElements;

        this.page = page <= 0 ? 1 : page;

        this.totalPages = totalElements == 0 ? 0 : totalElements % pageSize == 0 ? totalElements/pageSize : (totalElements/pageSize) + 1;

        this.first = page == 1 ? true : false;

        this.last = (page == this.totalPages || content.size() < 1) ? true : false;
        
        this.numberOfElements = this.content.size();

    }
    
    /**
     * 根据传入的当前多少页
     * @param pageSize
     * @param page
     * @param totalElements
     */
    public PageUtil(int page, int pageSize, int totalElements, List<?> content) {

        this.pageSize = pageSize;

        this.totalElements = totalElements;

        this.page = page <= 0 ? 1 : page;

        this.totalPages = totalElements == 0 ? 0 : totalElements % pageSize == 0 ? totalElements/pageSize : (totalElements/pageSize) + 1;

        this.first = page == 1 ? true : false;

        this.last = (page == this.totalPages || content.size() < 1) ? true : false;
        
        this.content = content;
        
        this.numberOfElements = this.content.size();

    }

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public boolean isFirst() {
		return first;
	}

	public void setFirst(boolean first) {
		this.first = first;
	}

	public boolean isLast() {
		return last;
	}

	public void setLast(boolean last) {
		this.last = last;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getTotalElements() {
		return totalElements;
	}

	public void setTotalElements(int totalElements) {
		this.totalElements = totalElements;
	}

	public int getNumberOfElements() {
		return numberOfElements;
	}

	public void setNumberOfElements(int numberOfElements) {
		this.numberOfElements = numberOfElements;
	}

	public List<?> getContent() {
		return content;
	}

	public void setContent(List<?> content) {
		this.content = content;
	}
}
